angular.module('app.baseinfo')
    .controller('materialCtrl', ['$scope', '$filter', '$modal', 'toaster', 'materialService',
        function ($scope, $filter, $modal, toaster, materialService) {
            $scope.query = "";
            $scope.content={};
            $scope.materials = {};
            $scope.content.skuClass={};
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };
            $scope.getSkuClass=function(){
                materialService.getSkuClass().then(function (res) {
                    $scope.skuClasses=[{"classDescription":"全部"}];
                    $scope.skuClasses =$scope.skuClasses.concat(res.data.data) ;
                    $scope.content.skuClass= $scope.skuClasses[0];
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getSkuClass();
            $scope.getMaterials = function () {
                var query = {
                    pageNo: $scope.pager.pageNo,
                    pageSize: $scope.pager.pageSize
                };
                if (query) {
                    query.query = $scope.query || "";
                    query.skuClass=$scope.content.skuClass.skuClass||"";
                }
                materialService.getMaterials(query).then(function (res) {
                    $scope.pager.totalItems = res.data.totalItems;
                    $scope.materials = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getMaterials();
            $scope.searchMaterials = function () {
                $scope.pager = {
                    pageNo: 1,
                    pageSize: 10,
                    totalItems: 0
                };
              $scope.getMaterials ();
            };


            $scope.addMaterial = function () {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/material-modal.html',
                    controller: 'addMaterialCtrl',
                    size: 'md',
                    resolve: {
                        material: function () {
                            return null;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getMaterials();
                });
            };

            $scope.updateMaterial = function (material) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/material-modal.html',
                    controller: 'addMaterialCtrl',
                    size: 'md',
                    resolve: {
                        material: function () {
                            return material;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getMaterials();
                });
            };

            $scope.deleteMaterial = function (material) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除？";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    materialService.deleteMaterial(material).then(function (res) {
                        toaster.pop("success", "成功", "删除物料成功");
                        $scope.getMaterials();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "删除物料失败");
                    });
                });
            };
        }])
    .controller('addMaterialCtrl', ['$scope', '$filter', '$modal', '$modalInstance', 'toaster', 'materialService','material','soService',
        function ($scope, $filter, $modal, $modalInstance, toaster, materialService,material,soService) {
            $scope.material = {};

            var getPacks=function(){
                soService.getPacks().then(function (res) {
                    $scope.packs = res.data.data;
                   if(material){
                        for(var i=0;i< $scope.packs.length;i++){
                            if($scope.packs[i].trim()== $scope.material.packMethod.trim()){
                                $scope.material.packMethod=$scope.packs[i];
                                break;
                            }
                        }
                    }

                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            getPacks();
            $scope.getSkuClass=function(){
                materialService.getSkuClass().then(function (res) {
                    $scope.skuClasses = res.data.data;
                    if(material){
                        for(var i=0;i<$scope.skuClasses.length;i++){
                            if($scope.skuClasses[i].classDescription.trim()== $scope.material.skuClass.trim()){
                                $scope.material.skuClass=$scope.skuClasses[i];
                                break;
                            }
                        }
                    }

                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };

            if(!material){
                $scope.title='新增物料';
                $scope.edit=false;
            }else{
                $scope.title='编辑物料';
                $scope.edit=true;
                angular.copy(material,$scope.material);
            }
            $scope.getSkuClass();
            $scope.ok = function () {
                $scope.material.skuClass=$scope.material.skuClass.skuClass;
                if(!material) {
                    materialService.addMaterial($scope.material).then(function (res) {
                        toaster.pop("success", "成功", "新增物料成功");
                        $modalInstance.close();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "新增物料失败");
                    });
                }else{
                    materialService.updateMaterial($scope.material).then(function (res) {
                        toaster.pop("success", "成功", "更新物料成功");
                        $modalInstance.close();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "更新物料失败");
                    });
                }
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);